package com.shioirikukuri.controller;

import com.shioirikukuri.entity.Gift;
import com.shioirikukuri.service.IGiftService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/gifts")
public class GiftController {

	@Autowired
	private IGiftService giftService;

	@GetMapping
	public List<Gift> getAllGifts() {
		return giftService.getAllGifts();
	}

	@GetMapping("/{id}")
	public Gift getGiftById(@PathVariable Long id) {
		return giftService.getGiftById(id);
	}

	@PostMapping
	public Gift addGift(@RequestBody Gift gift) {
		return giftService.addGift(gift);
	}

	@PutMapping("/{id}")
	public void updateGift(@PathVariable Long id, @RequestBody Gift giftDetails) {
		Gift gift = giftService.getGiftById(id);
		if (gift != null) {
			gift.setName(giftDetails.getName());
			gift.setDescription(giftDetails.getDescription());
			gift.setRequiredPoints(giftDetails.getRequiredPoints());
			gift.setStockQuantity(giftDetails.getStockQuantity());
			gift.setIsListed(giftDetails.getIsListed());
			giftService.updateGift(gift);
		}
	}

	@DeleteMapping("/{id}")
	public void deleteGift(@PathVariable Long id) {
		giftService.deleteGift(id);
	}
}